home *** CD-ROM | disk | FTP | other *** search
/ STraTOS 1997 April & May / STraTOS 1 - 1997 April & May.iso / CD01 / INTERNET / BROWSERS / GLUESTIK.ZOO / debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-27  |  2.7 KB  |  141 lines

  1. #include <support.h>
  2. #include <string.h>
  3. #include <osbind.h>
  4. #include <mintbind.h>
  5. #include <stdarg.h>
  6. #include <unistd.h>
  7. #include "global.h"
  8.  
  9. #ifdef DEBUG
  10.  
  11. #ifdef DAEMON
  12. #  define PROLOG    "GSDaemon: "
  13. #else
  14. #  ifdef DRIVER
  15. #    define PROLOG    "GSDriver: "
  16. #  else
  17. #    define PROLOG    ""
  18. #  endif
  19. #endif
  20.  
  21. static char DEBUG_LOG[] = "A:\\STIK_CFG\\GLUESTIK.LOG";
  22. static char DEBUG_OLD[] = "A:\\STIK_CFG\\GLUESTIK.OLD";
  23.  
  24. typedef char *charp;
  25. typedef void *voidp;
  26.  
  27. void debug(const char *fmt, ...)
  28. {
  29.   va_list ap;
  30.   register const char *s;
  31.   char *t;
  32.   void *v;
  33.   char buf[20];
  34.   long l;
  35.   int fd;
  36.   static int first_time = 1;
  37.  
  38.   Psemaphore(2, DEBUG_SEMAPHORE, -1L);
  39.   va_start(ap, fmt);
  40.   if (first_time) {
  41.     DEBUG_LOG[0] = DEBUG_OLD[0] = 'A' + Dgetdrv();
  42. #ifndef DRIVER
  43.     Fdelete(DEBUG_OLD);
  44.     l = Frename(0, DEBUG_LOG, DEBUG_OLD);
  45.     if (l == 0) {
  46.       Cconws("Preserved old debug log ");
  47.       Cconws(DEBUG_OLD);
  48.       Cconws("\r\n");
  49.     }
  50.     l = Fcreate(DEBUG_LOG, 0);
  51.     if (l < 0) {
  52.       Cconws("Unable to create debug log ");
  53.       Cconws(DEBUG_LOG);
  54.       Cconws("\r\n");
  55.       return;
  56.     }
  57.     Cconws("Debug log ");
  58.     Cconws(DEBUG_LOG);
  59.     Cconws(" created\r\n");
  60.     fd = (int)(short)l;
  61. #endif
  62.     first_time = 0;
  63.   }
  64. #ifndef DRIVER
  65.   else
  66. #endif
  67.   {
  68.     l = Fopen(DEBUG_LOG, 2);
  69.     if (l < 0) {
  70.       Cconws("Unable to open debug log ");
  71.       Cconws(DEBUG_LOG);
  72.       Cconws("\r\n");
  73.       return;
  74.     }
  75.     fd = (int)(short)l;
  76.     Fseek(0L, fd, SEEK_END);
  77.   }
  78.  
  79.   Fwrite(fd, strlen(PROLOG) + 4, PROLOG "pid ");
  80.   _itoa(getpid(), buf, 10);
  81.   Fwrite(fd, strlen(buf), buf);
  82.   Fwrite(fd, 3, ":  ");
  83.  
  84.   for (s = fmt; *s; s++) {
  85.     switch (*s) {
  86.       case 's':
  87.     t = va_arg(ap, charp);
  88.     Fwrite(fd, strlen(t), t);
  89.     break;
  90.       case 'S':
  91.     l = (long)va_arg(ap, int);
  92.     t = va_arg(ap, charp);
  93.     Fwrite(fd, l, t);
  94.     break;
  95.       case 'i':
  96.       case 'd':
  97.     l = (long)va_arg(ap, int);
  98.     _ltoa(l, buf, 10);
  99.     Fwrite(fd, strlen(buf), buf);
  100.     break;
  101.       case 'x':
  102.     Fwrite(fd, 2, "0x");
  103.     l = (long)va_arg(ap, unsigned int);
  104.     _ltoa(l, buf, 16);
  105.     Fwrite(fd, strlen(buf), buf);
  106.     break;
  107.       case 'l':
  108.     l = va_arg(ap, long);
  109.     _ltoa(l, buf, 10);
  110.     Fwrite(fd, strlen(buf), buf);
  111.     break;
  112.       case 'X':
  113.     Fwrite(fd, 2, "0x");
  114.     l = va_arg(ap, unsigned long);
  115.     _ultoa(l, buf, 16);
  116.     Fwrite(fd, strlen(buf), buf);
  117.     break;
  118.       case 'p':
  119.     Fwrite(fd, 2, "0x");
  120.     v = va_arg(ap, voidp);
  121.     _ultoa((unsigned long)v, buf, 16);
  122.     Fwrite(fd, strlen(buf), buf);
  123.     break;
  124.       case 'c':
  125.     buf[0] = va_arg(ap, int);
  126.     Fwrite(fd, 1, buf);
  127.     break;
  128.       default:
  129.     Fwrite(fd, 3, "<<<");
  130.     Fwrite(fd, 1, s);
  131.     Fwrite(fd, 4, "?>>>");
  132.     break;
  133.     }
  134.   }
  135.   Fwrite(fd, 2, "\r\n");
  136.   Fclose(fd);
  137.   Psemaphore(3, DEBUG_SEMAPHORE, 0L);
  138. }
  139.  
  140. #endif /* DEBUG */
  141.